#!/usr/bin/env python

import pygame, sys
import statemanager

class Geeno():
        
    def __init__(self):
        self.read_config()
        
        #initialize pygame options
        pygame.init()
        pygame.mixer.init()
        self._screen = pygame.display.set_mode((self.sWidth, self.sHeight))
        pygame.display.set_caption("Project Geeno")
        self._clock = pygame.time.Clock()
        
        #set main menu as current gamestate
        self._gameState = statemanager.StateManager((self.sWidth, self.sHeight))
        
        #establish game as running
        self.quit = False
        
        #event types to be discarded, not passed to the statemanager
        self._discardEvents = [pygame.MOUSEBUTTONDOWN,
                               pygame.MOUSEBUTTONUP,
                               pygame.MOUSEMOTION]
        
    def play_sound(self, sound):
        """If sounds are turned on, play requested sound"""
        if self._soundsOn:
            sound.play()
        
    def read_config(self):
        """Reads config options from external file"""
        print('Reading config file')
        
        self._soundsOn = False
        self.sHeight = 768
        self.sWidth = 1024
        
        print('Read config file')
        
    def run(self):
        """Main running method of program"""        
        while self._gameState.is_running() and not self.quit:
            #have gamestate handle drawing to screen
            self._gameState.draw(self._screen)
    
            #30 fps
            self._clock.tick(30)
            
            #handle events, locally then allow _gameState to handle the rest
            #discard selected event types
            pygame.event.get(self._discardEvents)
            
            self.quit = pygame.event.get(pygame.QUIT)
            (self._gameState.handle_events(pygame.event.get(),
                                           pygame.key.get_mods()))
            
            #update at end of loop because update is where statemanager changes
            #running status
            self._gameState.update(self._clock.get_time())
            
if __name__ == '__main__':
    #confirm user has Python version 3 or higher
    if (sys.version_info.major < 3):
        print("Python 3 or greater is required.")
        sys.exit(1)
        
    Geeno().run()
    print("Exiting...")
    pygame.quit()